package com.joymart.admin.rest;


import com.joymart.common.error.handling.JoymartBusinessException;
import com.joymart.common.model.ApprovalStatus;
import com.joymart.common.model.Range;
import com.joymart.common.utils.SecurityUtils;
import com.joymart.qualification.model.QualificationApplication;
import com.joymart.qualification.service.ApplicationService;
import com.joymart.user.model.BusinessRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Optional;

@RestController
@RequestMapping("/admin")
public class QualificationAdminController {

    @Autowired
    private ApplicationService applicationService;

    @GetMapping("/{targetRole}/applications")
    public Object search(@PathVariable BusinessRole targetRole,
                         @RequestParam(required = false) String keyword,
                         @RequestParam(required = false) ApprovalStatus targetStatus,
                         @RequestParam LocalDateTime start,
                         @RequestParam LocalDateTime end,
                         @RequestParam(value = "pageNo", defaultValue = "0")Integer pageNo,
                         @RequestParam(value = "pageSize", defaultValue = "20")Integer pageSize) {
        Range range = Range.of(start, end);
        Pageable pageable = PageRequest.of(pageNo, pageSize);
        return applicationService.searchApplication(targetRole, targetStatus, keyword, range, pageable);
    }

    @GetMapping("/{targetRole}/applications/{id}")
    public Object detail(@PathVariable BusinessRole targetRole, @PathVariable String id) {
        return Optional.ofNullable(applicationService.findById(id))
                .filter(application -> application.getTargetRole().equals(targetRole))
                .map(applicationService::toDto)
                .orElse(null);
    }

    @PutMapping(value = "/{targetRole}/applications/{id}", params = "action=APPROVE")
    public Object approve(@PathVariable BusinessRole targetRole, @PathVariable String id) {
      SecurityUtils.requiresRestAuthority("QualificationApproval");
      QualificationApplication qualificationApplication = Optional.ofNullable(applicationService.findById(id))
                .filter(application -> application.getTargetRole().equals(targetRole))
                .orElseThrow(()-> new JoymartBusinessException("ID或类型错误"));
        applicationService.approveApplication(id);
        return this.detail(targetRole, qualificationApplication.getId());
    }

    @PutMapping(value = "/{targetRole}/applications/{id}", params = "action=REJECT")
    public Object reject(@PathVariable BusinessRole targetRole, @PathVariable String id,
                         @RequestParam(required = false, defaultValue = "") String comment) {

        SecurityUtils.requiresRestAuthority("QualificationApproval");
        QualificationApplication qualificationApplication = Optional.ofNullable(applicationService.findById(id))
                .filter(application -> application.getTargetRole().equals(targetRole))
                .orElseThrow(()-> new JoymartBusinessException("ID或类型错误"));
        applicationService.rejectApplication(id, comment);
        return this.detail(targetRole, qualificationApplication.getId());
    }

}
